<html>
<head><meta charset="utf-8"><title>From&lt;&amp;String&gt; for &amp;str · t-libs · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/index.html">t-libs</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/From.3C.26String.3E.20for.20.26str.html">From&lt;&amp;String&gt; for &amp;str</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="222058306"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/From%3C%26String%3E%20for%20%26str/near/222058306" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> rylev <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/From.3C.26String.3E.20for.20.26str.html#222058306">(Jan 08 2021 at 10:46)</a>:</h4>
<p>Is it possible to add an implementation of <code>&lt;'a&gt; From&lt;&amp;'a String&gt; for &amp;'a str</code> to the std library or is there an explicit reason it's not there?</p>



<a name="222066553"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/From%3C%26String%3E%20for%20%26str/near/222066553" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> davidtwco <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/From.3C.26String.3E.20for.20.26str.html#222066553">(Jan 08 2021 at 12:26)</a>:</h4>
<p>I'll move this topic to <a class="stream" data-stream-id="219381" href="/#narrow/stream/219381-t-libs">#t-libs</a> since that's probably a more relevant stream <span aria-label="smile" class="emoji emoji-1f642" role="img" title="smile">:smile:</span></p>



<a name="222066568"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/From%3C%26String%3E%20for%20%26str/near/222066568" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Notification Bot <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/From.3C.26String.3E.20for.20.26str.html#222066568">(Jan 08 2021 at 12:26)</a>:</h4>
<p>This topic was moved here from <a class="stream-topic" data-stream-id="122651" href="/#narrow/stream/122651-general/topic/From.3C.26String.3E.20for.20.26str">#general &gt; From&lt;&amp;String&gt; for &amp;str</a> by <span class="user-mention silent" data-user-id="116107">davidtwco</span></p>



<a name="222118061"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/From%3C%26String%3E%20for%20%26str/near/222118061" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Simon Sapin <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/From.3C.26String.3E.20for.20.26str.html#222118061">(Jan 08 2021 at 19:58)</a>:</h4>
<p>I think this conversion is just <code>&amp;**x</code> ?</p>



<a name="222121009"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/From%3C%26String%3E%20for%20%26str/near/222121009" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> cuviper <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/From.3C.26String.3E.20for.20.26str.html#222121009">(Jan 08 2021 at 20:24)</a>:</h4>
<p>there's <code>Deref</code>, <code>AsRef&lt;str&gt;</code>, <code>Borrow&lt;str&gt;</code>, or the inherent <code>String::as_str()</code></p>



<a name="222121441"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/From%3C%26String%3E%20for%20%26str/near/222121441" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> cuviper <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/From.3C.26String.3E.20for.20.26str.html#222121441">(Jan 08 2021 at 20:28)</a>:</h4>
<p>I don't see any precedent of <code>From</code> for a reference</p>



<a name="222128945"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/From%3C%26String%3E%20for%20%26str/near/222128945" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lokathor <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/From.3C.26String.3E.20for.20.26str.html#222128945">(Jan 08 2021 at 21:33)</a>:</h4>
<p>yeah, <code>s.as_str()</code> seems like the right move</p>



<a name="222133874"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/From%3C%26String%3E%20for%20%26str/near/222133874" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/From.3C.26String.3E.20for.20.26str.html#222133874">(Jan 08 2021 at 22:21)</a>:</h4>
<p>there's already enough ways to convert between strings <span aria-label="sweat smile" class="emoji emoji-1f605" role="img" title="sweat smile">:sweat_smile:</span></p>



<a name="222181274"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/From%3C%26String%3E%20for%20%26str/near/222181274" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> rylev <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/From.3C.26String.3E.20for.20.26str.html#222181274">(Jan 09 2021 at 17:58)</a>:</h4>
<p>This is for generic code where an API takes Into&lt;&amp;str&gt;.</p>



<a name="222182003"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/From%3C%26String%3E%20for%20%26str/near/222182003" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> cuviper <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/From.3C.26String.3E.20for.20.26str.html#222182003">(Jan 09 2021 at 18:13)</a>:</h4>
<p><code>Into&lt;&amp;str&gt;</code> is a strange choice though, because I don't know anything besides the reflexive impl for that</p>



<a name="222182015"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/From%3C%26String%3E%20for%20%26str/near/222182015" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> cuviper <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/From.3C.26String.3E.20for.20.26str.html#222182015">(Jan 09 2021 at 18:13)</a>:</h4>
<p><code>AsRef&lt;str&gt;</code> would be more typical</p>



<a name="222215716"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/From%3C%26String%3E%20for%20%26str/near/222215716" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> rylev <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/From.3C.26String.3E.20for.20.26str.html#222215716">(Jan 10 2021 at 10:18)</a>:</h4>
<p>I totally get that. I do think this is a slight usability papercut. i just noticed that most of my APIs take Into&lt;T&gt; for there parameters but when T=&amp;str I have to switch to a different trait. I get why things are the way they are, but it would be nice if there was one uniform way to build APIs that took parameters that could turn into the value that I ultimately care about.</p>



<a name="222219732"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/From%3C%26String%3E%20for%20%26str/near/222219732" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/From.3C.26String.3E.20for.20.26str.html#222219732">(Jan 10 2021 at 12:16)</a>:</h4>
<p><span class="user-mention" data-user-id="224872">@rylev</span> even after this change it wouldn't be uniform because you can't take String, only &amp;String. And there's no way to write <code>impl From&lt;String&gt; for &amp;str</code> because of lifetimes</p>



<a name="222343310"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/From%3C%26String%3E%20for%20%26str/near/222343310" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> rylev <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/From.3C.26String.3E.20for.20.26str.html#222343310">(Jan 11 2021 at 19:12)</a>:</h4>
<p>That’s true. It’s unfortunate, but I understand why it is how it is. 😀</p>



<a name="222349458"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/From%3C%26String%3E%20for%20%26str/near/222349458" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> cuviper <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/From.3C.26String.3E.20for.20.26str.html#222349458">(Jan 11 2021 at 19:58)</a>:</h4>
<p>Use <code>Into&lt;_&gt;</code> for owned values, and <code>AsRef&lt;_&gt;</code> or <code>AsMut&lt;_&gt;</code> for borrowed references.</p>



<a name="222349486"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/From%3C%26String%3E%20for%20%26str/near/222349486" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> cuviper <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/From.3C.26String.3E.20for.20.26str.html#222349486">(Jan 11 2021 at 19:58)</a>:</h4>
<p>just as APIs have to decide between <code>self</code>, <code>&amp;self</code>, and <code>&amp;mut self</code></p>



<a name="223986582"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/From%3C%26String%3E%20for%20%26str/near/223986582" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> enterprisey <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/From.3C.26String.3E.20for.20.26str.html#223986582">(Jan 26 2021 at 01:22)</a>:</h4>
<p>Is this worth mentioning in the docs for Into?</p>



<a name="224162743"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/From%3C%26String%3E%20for%20%26str/near/224162743" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> rylev <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/From.3C.26String.3E.20for.20.26str.html#224162743">(Jan 27 2021 at 10:29)</a>:</h4>
<p>So I've been thinking more about this, and I think what <span class="user-mention" data-user-id="138448">@cuviper</span> is correct though slightly misleading. Let's say I have a function <code>foo</code> that takes some argument <code>T: 'a</code> and returns some result <code>MyStruct&lt;'a&gt;</code>. The argument is an owned value but with lifetime <code>'a</code>. If we want to make the parameter more flexible we can change it to <code>Into&lt;T&gt;</code> where <code>T: 'a</code>. <br>
Now let's move to the specific case of <code>T</code> being <code>&amp;'a str</code>. If I need to move the string slice into the return result I can't use <code>AsRef</code> as the lifetime of the borrow is contrained to the lifetime on the call to <code>as_ref()</code>. If there was an implementation for <code>From&lt;&amp;'a String&gt; for &amp;'a str</code> I could use <code>Into&lt;&amp;'a str&gt;</code> as the generic paramter.</p>



<a name="224215273"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/From%3C%26String%3E%20for%20%26str/near/224215273" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> cuviper <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/From.3C.26String.3E.20for.20.26str.html#224215273">(Jan 27 2021 at 17:22)</a>:</h4>
<p><span class="user-mention" data-user-id="224872">@rylev</span> my quick take on that is that we might want to be more general, something like:</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">impl</span><span class="o">&lt;'</span><span class="na">a</span><span class="p">,</span><span class="w"> </span><span class="n">T</span>: <span class="nc">Deref</span><span class="o">&gt;</span><span class="w"> </span><span class="nb">From</span><span class="o">&lt;&amp;'</span><span class="na">a</span><span class="w"> </span><span class="n">T</span><span class="o">&gt;</span><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="o">&amp;'</span><span class="na">a</span><span class="w"> </span><span class="n">T</span>::<span class="n">Target</span><span class="w"> </span><span class="p">{</span><span class="o">..</span><span class="p">.}</span><span class="w"></span>
</code></pre></div>



<a name="224215283"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/From%3C%26String%3E%20for%20%26str/near/224215283" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> cuviper <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/From.3C.26String.3E.20for.20.26str.html#224215283">(Jan 27 2021 at 17:22)</a>:</h4>
<p>but I think that would require lattice specialization</p>



<a name="224215513"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/From%3C%26String%3E%20for%20%26str/near/224215513" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> cuviper <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/From.3C.26String.3E.20for.20.26str.html#224215513">(Jan 27 2021 at 17:24)</a>:</h4>
<p>If you have a call taking <code>arg: T</code> where <code>T</code> is already constrained to <code>&amp;str</code>, then <code>&amp;String</code> should already work by deref coercion. But I think that will go out the window as soon as you change it to <code>arg: impl Into&lt;T&gt;</code>.</p>



<a name="224215691"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/From%3C%26String%3E%20for%20%26str/near/224215691" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> cuviper <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/From.3C.26String.3E.20for.20.26str.html#224215691">(Jan 27 2021 at 17:25)</a>:</h4>
<p>the <code>impl</code> above would sort of codify part of deref coercion in <code>From</code>, if that's possible</p>



<a name="224216345"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/From%3C%26String%3E%20for%20%26str/near/224216345" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> cuviper <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/From.3C.26String.3E.20for.20.26str.html#224216345">(Jan 27 2021 at 17:29)</a>:</h4>
<p>Maybe <code>String</code> is special enough to justify its own deref-like <code>From</code>, or maybe we should do it individually for a lot of <code>Deref</code> types</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>